/*
 * Copyright (c) 2020. bmy
 */

import Vue from 'vue';
import { ConfigureConfig } from "@config/configure.config";
import { CreateElement } from 'vue/types/umd';
import Es6Promise from 'es6-promise'

Es6Promise.polyfill()

export function Start<T extends { new ( ...args: any[] ): { } }>( _ : T ) : T {
  // @ts-ignore
  return class extends _ {
    constructor() {
      super();
      
      (new Vue({
        // @ts-ignore
        router: this.Router, store: this.VueX, render: (h: CreateElement) => h(this.app)
      }) as Vue).$mount('#app')
    }
  }
}

export function RegServiceAndMethods(constructor: { new(...args: any[]): {}; }) {
  // newInstance(require.context("../service/impl", false, /\.ts$/))
  newInstance(require.context("../utils", false, /\.ts$/))
}

function newInstance(context: __WebpackModuleApi.RequireContext) {
  context.keys().forEach((key: any) => {
    let m = context(key), className: string = Object.keys(m)[0];
    new m[className]()
  });
}


/**
 * 自动注册Vue插件
 * @constructor
 */
export function RegVuePlugs(constructor: { new(...args: any[]): {}; }) {
  ConfigureConfig.VuePlugs.forEach((v: any) => Vue.use(v))
}

/**
 * 自动挂载Vue 全局方法
 * @constructor
 */
export function GlobalMethod(target: any, propertyKey: string) {
  Vue.prototype[`$${ propertyKey }`] = target[propertyKey]
}
